package com.itheima.project.face.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.project.constant.log.LogCacheConstant;
import com.itheima.project.core.enums.log.LogBusinessEnum;
import com.itheima.project.exception.ProjectException;
import com.itheima.project.face.LogBusinessFace;
import com.itheima.project.pojo.LogBusiness;
import com.itheima.project.service.ILogBusinessService;
import com.itheima.project.utils.BeanConv;
import com.itheima.project.utils.ExceptionsUtil;
import com.itheima.project.vo.log.LogBusinessVo;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description：日志dubbo服务实现
 */
@Slf4j
@Component
public class LogBusinessFaceImpl implements LogBusinessFace {

    @Autowired
    ILogBusinessService logBusinessService;

    @Override
    @Cacheable(value = LogCacheConstant.PAGE,key ="#pageNum+'-'+#pageSize+'-'+#logBusinessVo.hashCode()")
    public Page<LogBusinessVo> findLogBusinessVoPage(LogBusinessVo logBusinessVo, int pageNum,
                                                     int pageSize) throws ProjectException {
        try {
            Page<LogBusiness> page = logBusinessService.findLogBusinessVoPage(logBusinessVo, pageNum, pageSize);
            return BeanConv.toPage(page,LogBusinessVo.class);
        } catch (Exception e) {
            log.error("查询日志列表异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(LogBusinessEnum.PAGE_FAIL);
        }

    }

    @Override
    @CacheEvict(value = LogCacheConstant.PAGE,allEntries = true)
    @Transactional
    public Boolean saveLogBusiness(LogBusinessVo logBusinessVo) {
        try {
            return logBusinessService.save(BeanConv.toBean(logBusinessVo,LogBusiness.class));
        } catch (Exception e) {
            log.error("查询日志列表异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(LogBusinessEnum.SAVE_FAIL);
        }
    }
}
